package com.bawei.week.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.entity.ParkBeanItem
import com.bawei.lib_base.mvvm.BaseActivity
import com.bawei.lib_base.utils.Cons
import com.bawei.week.R
import com.bawei.week.adapter.OnItemClickListener
import com.bawei.week.adapter.OnItemLongClickListener
import com.bawei.week.adapter.ParkAdapter
import com.bawei.week.databinding.ActivityParkBinding
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding,ParkViewModel>() {
    val parkAdapter by lazy { ParkAdapter() }
    var type = "类型"
    var status = ""
    var addId = "-1"
    var addName = ""
    override fun initView() {
        binding.rvPark.apply {
            adapter = parkAdapter
            layoutManager = LinearLayoutManager(this@ParkActivity)
        }

        //点击添加
        binding.ivAdd.setOnClickListener {
            ARouter.getInstance().build("/app/ParkAddActivity").navigation()
        }

        //点击修改车位
        parkAdapter.setOnItemChildClick(object : OnItemClickListener{
            override fun onItemClick(position: Int, bean: ParkBeanItem) {
                ARouter.getInstance().build("/app/ParkModifyActivity")
                    .withInt("parkId",bean.id).navigation()
            }
        })

        //长按进入车辆VR页面
        parkAdapter.setOnItemChildLongClick(object : OnItemLongClickListener{
            override fun onItemClick(bean: ParkBeanItem) {
                ARouter.getInstance().build("/app/ParkVrActivity").navigation()
            }
        })

        //根据搜索条件搜索
        binding.parkSearchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                addId = query!!
                if (addName.isNotEmpty()){
                    lifecycleScope.launch {
                        vm.getParkByType(addId,addName).collect{
                            parkAdapter.submitData(it)
                        }
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }
        })

        //显示副窗口
        binding.parkFilter.setOnClickListener {
            binding.drawer.openDrawer(GravityCompat.END)
        }

        //根据区域搜索
        binding.drawArea.setOnClickListener {
            addName = "区域"
            binding.drawer.closeDrawer(GravityCompat.END)
            if (addId == "-1"){
                ToastUtils.showShort("请输入区域Id")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                vm.getParkByType(addId,addName).collect{
                    parkAdapter.submitData(it)
                }
            }
        }

        //根据车牌号搜索
        binding.drawCarNo.setOnClickListener {
            addName = "车牌号"
            binding.drawer.closeDrawer(GravityCompat.END)
            if (addId == "-1"){
                ToastUtils.showShort("请输入车牌号")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                vm.getParkByType(addId,addName).collect{
                    parkAdapter.submitData(it)
                }
            }
        }

        //根据类型搜索
        binding.drawType.setOnClickListener {
            addName = "类型"
            binding.drawer.closeDrawer(GravityCompat.END)
            if (addId == "-1"){
                ToastUtils.showShort("请输入车位类型")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                vm.getParkByType(addId,addName).collect{
                    parkAdapter.submitData(it)
                }
            }
        }

        //通过状态刷新列表
        binding.drawRg.setOnCheckedChangeListener { radioGroup, chickId ->
            if (chickId == R.id.rb_kx){
                status = "空闲"
            }else if (chickId == R.id.rb_zy){
                status = "已占用"
            }else if (chickId == R.id.rb_wh){
                status = "维修中"
            }
            binding.drawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                vm.getParkByType(status,"状态").collect{
                    parkAdapter.submitData(it)
                }
            }
        }
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            vm.getParkByType("标准车位",type).collect{
                parkAdapter.submitData(it)
            }
        }
    }
}